#!/bin/bash
#功能：导出单一车的0200部分数据

CUR_DIR=$(cd "$(dirname "$0")"; pwd)
if [ $# -ge 2 ]; then
    echo "Shell 传递参数$#个！"
	echo "执行的文件名：$0"
	echo "第一个参数为：$1"
	echo "第二个参数为：$2"
else
    echo "参数错误！"
    echo "Shell 传递参数$#个！"
    echo "第一个参数为：termianlid"
    echo "第二个参数为：生成文件的地址"
    exit 1
fi

#步骤一参数准备
CUR_DIR=$(cd "$(dirname "$0")"; pwd)

terminalid=$1
target_dir=$2

if [ ! -d $target_dir ];then
   echo "路径不存在,创建路径"
   mkdir -p $target_dir
fi

hive_config=$(cat  "${CUR_DIR}"/config/hive.config)

#步骤二，0200数据写入导出的临时表
import_sql_suffix=$(cat <<EOF
	insert overwrite table dataplatform_huashen.ods_location_data_0200_export_temp partition(part_time,hashtid)
	SELECT
         lc.longitude,
         lc.latitude,
         lc.originallng,
         lc.originallat,
         lc.height,
         lc.speed,
         lc.direction,
         lc.gpsdate,
         lc.mileage,
         lc.oil,
         lc.statusadditionStr,
         lc.standardmileage,
         lc.standardfuelcon,
         car.car_model,
         lc.terminalid,
         lc.part_time as part_time,
         lc.hashtid as hashtid
	from(
	 SELECT
	      collect_set(longitude)[0] as longitude,
	      collect_set(latitude)[0] as latitude,
	      collect_set(originallng)[0] as originallng,
	      collect_set(originallat)[0] as originallat,
	      collect_set(height)[0] as height,
	      collect_set(speed)[0] as speed,
	      collect_set(direction)[0] as direction,
	      gpsdate as gpsdate,
	      collect_set(mileage)[0] as mileage,
	      collect_set(oil)[0] as oil,
	      concat_ws(',',collect_list(concat(statusjson.types,"_",statusjson.statusvalue))) as statusadditionStr,
	      collect_set(standardmileage)[0] as standardmileage,
	      collect_set(standardfuelcon)[0] as standardfuelcon,
	      terminalid as terminalid,
	      collect_set(part_time)[0] as part_time,
	      collect_set(hashtid)[0] as hashtid
	   FROM dataplatform_huashen.ods_location_data_0200_pdi
	   LATERAL VIEW explode (statusaddition.status) addTable AS statusjson
	   group by terminalid,gpsdate
	)lc
    left join dataplatform_huashen.ods_hy_car_terminal_temp car on car.T_COMMUNICATION_ID = lc.terminalid
	WHERE hashtid= pmod(${terminalid},32) and terminalid= ${terminalid} distribute by part_time,hashtid;
EOF
)

import_sql="${hive_config}${import_sql_suffix}"
echo "写入数据到临时表SQL为：${import_sql}"
echo "写入数据到临时表"
hive -e "${import_sql}"
echo "写入数据到临时表完成"

echo "导出数据到csv开始"

export_sql=$(cat << EOF
set hive.cli.print.header=true;
select terminalid ,gpsdate, car_model as cx, longitude as jd,latitude as wd,mileage as zlc,standardmileage as lcbz,standardfuelcon as bzyh,status_arr.realtimeFuelConsumption as ssyh,status_arr.totalFuelCons as ryzxh from dataplatform_huashen.ods_location_data_0200_export_temp LATERAL VIEW dataplatform_huashen.status_addition(statusaddition) status_arr\
  WHERE hashtid= pmod(${terminalid},32) and terminalid= ${terminalid};
EOF
)

temp_sql_file=${terminalid}.sql
echo ${export_sql} > ${temp_sql_file}
cat ${temp_sql_file}

hive -f ${temp_sql_file}  | sed -e 's/\t/,/g' -e 's/cx/"车型"/g' -e 's/jd/"经度"/g' -e 's/wd/"纬度"/g' -e 's/zlc/"总里程"/g' -e 's/lcbz/"标准里程"/g' -e 's/bzyh/"标准油耗"/g' -e 's/ssyh/"瞬时油耗"/g' -e 's/ryzxh/"燃油总消耗"/g'  >${target_dir}/${terminalid}.csv
echo "导出数据到csv结束"

rm -rf ${temp_sql_file}
